import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// 汽车接口
interface Car {
    String assemble();
    String paint();
    String polish();
}

// 轿车类实现汽车接口
class Sedan implements Car {
    @Override
    public String assemble() {
        return "Assembling Sedan...\n";
    }

    @Override
    public String paint() {
        return "Painting Sedan...\n";
    }

    @Override
    public String polish() {
        return "Polishing Sedan...\n";
    }
}

// SUV类实现汽车接口
class SUV implements Car {
    @Override
    public String assemble() {
        return "Assembling SUV...\n";
    }

    @Override
    public String paint() {
        return "Painting SUV...\n";
    }

    @Override
    public String polish() {
        return "Polishing SUV...\n";
    }
}

// 轿车工厂类
class SedanFactory {
    // 生产轿车
    public static Car createCar() {
        return new Sedan();
    }
}

// SUV工厂类
class SUVFactory {
    // 生产SUV
    public static Car createCar() {
        return new SUV();
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            // 从配置文件中读取要生产的汽车类型
            String carType = ReadCarTypeFromXML();

            // 根据汽车类型选择对应的工厂
            Car car;
            if (carType.equalsIgnoreCase("sedan")) {
                car = SedanFactory.createCar();
            } else if (carType.equalsIgnoreCase("suv")) {
                car = SUVFactory.createCar();
            } else {
                throw new IllegalArgumentException("Invalid car type: " + carType);
            }

            // 生产汽车并输出生产过程
            System.out.println(car.assemble());
            System.out.println(car.paint());
            System.out.println(car.polish());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    // 从XML配置文件中读取要生产的汽车类型
    public static String ReadCarTypeFromXML() {
        String carType = "";
        try {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("src//config.xml"));

            NodeList nl = doc.getElementsByTagName("carType");
            Node carNode = nl.item(0).getFirstChild();
            carType = carNode.getNodeValue();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return carType;
    }
}
